package service

import (
	"context"
	"errors"

	"goserver/internal/model"
	"golang.org/x/crypto/bcrypt"
)

type UserService struct {
	userModel *model.UserModel
}

func NewUserService(userModel *model.UserModel) *UserService {
	return &UserService{
		userModel: userModel,
	}
}

// GetUserByID 根据ID获取用户信息
func (s *UserService) GetUserByID(ctx context.Context, userID int) (*model.User, error) {
	return s.userModel.GetUserByID(ctx, userID)
}

// UpdateUser 更新用户信息
func (s *UserService) UpdateUser(ctx context.Context, user *model.User) error {
	return s.userModel.UpdateUser(ctx, user)
}

// UpdatePassword 修改密码
func (s *UserService) UpdatePassword(ctx context.Context, userID int, oldPassword, newPassword string) error {
	// 获取用户信息
	user, err := s.userModel.GetUserByID(ctx, userID)
	if err != nil {
		return err
	}

	// 验证旧密码
	if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(oldPassword)); err != nil {
		return errors.New("旧密码错误")
	}

	// 更新密码
	return s.userModel.UpdatePassword(ctx, userID, newPassword)
} 